package security.LoginSystem.Base;

import security.Authentication.Base.AuthenticationChain;
import security.Authentication.Base.AuthenticationHandler;
import security.Authentication.Base.AuthenticationRequest;
import security.Authentication.Base.User;
import security.Authorization.Base.AuthorizationHandler;
import security.IntrusionDetection.Base.IntrusionDetection;

/**
 * A login system with authentication, authorization and intrusion detection.
 * The login system can contain only one authorization handler.
 * For authentication and intrusion detection any number of handlers can be
 * added.
 * The priority of the authentication and intrusion detection handlers
 * will be in the order it was added. If a intrusion detection is added
 * before an authentication handler the authentication handler will be
 * protected by the intrusion detection.
 * @author Andreas Carmvall
 */
public class LoginSystem {

	/**
	 * A chain of responsibility to take care of authentication and
	 * intrusion detection
	 */
	private AuthenticationChain chain = new AuthenticationChain();
	
	/**
	 * The authorization handler to be used
	 */
	private AuthorizationHandler authoHandler;

	/**
	 * Try to login
	 * @param request the request to be used
	 */
	public void login(AuthenticationRequest request) {
		User user = chain.handleRequest(request);
		
		if (authoHandler != null) {
			authoHandler.authorize(user);
		}
	}
	
	/**
	 * Add a new authentication handler to the chain
	 * @param autheHandler the authentication hander to be added
	 */
	public void addAuthentication(AuthenticationHandler autheHandler) {
		chain.setNext(autheHandler);
	}
	
	/**
	 * Add a new intrusion detection to the chain
	 * @param intDect the intrusion detection to be added
	 */
	public void addIntrusionDetection(IntrusionDetection intDect) {
		chain.setNext(intDect);
	}
	
	/**
	 * Set the authorization handler
	 * @param authoHandler the authorization handler to be used
	 */
	public void setAuthorization(AuthorizationHandler authoHandler) {
		this.authoHandler = authoHandler;
	}
	
	/**
	 * Clear the chain
	 */
	public void clearChain() {
		chain.clear();
	}
}
